/**
 * 管理员
 */
package com.zhaogang.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.zhaogang.dto.AdminOrderDto;
import com.zhaogang.dto.AdminSaleDto;
import com.zhaogang.dto.AdminUserDto;
import com.zhaogang.dto.SaleSettleDetailDTO;
import com.zhaogang.entity.AdminAccount;
import com.zhaogang.entity.Page;
import com.zhaogang.entity.SaleAccount;
import com.zhaogang.entity.SaleSettleinfo;
import com.zhaogang.entity.UserAccount;
import com.zhaogang.service.AdminLoginService;
import com.zhaogang.service.OrderService;
import com.zhaogang.service.SaleAccountService;
import com.zhaogang.service.SaleSettleinfoService;
import com.zhaogang.service.UserAccountService;

@Controller
@RequestMapping(value = "/admin")
public class AdminController {

	@Resource
	private SaleSettleinfoService saleSettleinfoService;
	@Resource
	private SaleAccountService saleAccountService;
	@Resource
	private UserAccountService userAccountService;
	@Resource
	private OrderService orderService;
	@Resource
	private AdminLoginService adminLoginService;
	//查错了，应该查商家账号表的
	@RequestMapping(value="/list1")
	public ModelAndView list1(){
		ModelAndView view = new ModelAndView();
		SaleSettleinfo record = new SaleSettleinfo();//空对象，表示查询所有		
		List<SaleSettleinfo> SaleSettleinfoList = saleSettleinfoService.selectInfoList(record);
		for (SaleSettleinfo saleSettleinfo : SaleSettleinfoList) {
			System.out.println(saleSettleinfo);
		}
		view.addObject("SaleSettleinfoList", SaleSettleinfoList);
		view.setViewName("admin/home");
		return view;
	}
	
	@RequestMapping(value="/list")
	public ModelAndView list(){
		ModelAndView view = new ModelAndView();
		List<SaleAccount> SaleAccountList = saleAccountService.selectAllSaleAccountList();
		System.out.println(SaleAccountList);
		//view.addObject("SaleAccountList",SaleAccountList);
		
		List<SaleSettleDetailDTO> list = saleSettleinfoService.queryListForApplyDetail();
		System.out.println(list);
		view.addObject("SaleSettleDetailDTOList",list);
		view.setViewName("admin/applyManage");
		return view;
	}
	
	@RequestMapping(value="/ajaxForUpdateState")
	//@ResponseBody  将异步刷新改为请求
	public String update(Integer id,Integer key){
		
		SaleSettleinfo record = new SaleSettleinfo();
		record.setSaleAccountId(id);
		record.setState(key);
		
		if(key == 1){
			record.setRemark("通过！");
		}else if(key == 2){
			record.setRemark("不通过！");
		}
		try{
			saleSettleinfoService.updateState(record);
		}catch(Exception e){
			
		}
		return "redirect:/admin/list";
	}
	
	
	@RequestMapping(value="/ajaxForSaleAccount")
	@ResponseBody
	public SaleAccount detailForSaleAccount(Integer id){
		
		SaleAccount saleAccount = saleAccountService.selectByPrimaryKey(id);
		System.out.println("saleAccount:"+saleAccount);
		
		return saleAccount;
	}

	@RequestMapping(value="/ajaxForSaleSettleInfo")
	@ResponseBody
	public SaleSettleinfo detailForSaleSettleInfo(Integer id){
		
		SaleSettleinfo saleSettleinfo = saleSettleinfoService.selectByAccountId(id);
		System.out.println("saleSettleinfo:"+saleSettleinfo);
		
		return saleSettleinfo;
	}
	
	
	/**
	 * 管理员登录
	 */
	@RequestMapping(value="/login",method=RequestMethod.POST)
	public String adminLogin(HttpServletRequest request,HttpServletResponse response){
		String loginName=request.getParameter("username");
		String loginPwd=request.getParameter("password");
		try {
		AdminAccount account=new AdminAccount();
		account.setAdminAccount(loginName);
		account.setPassWord(loginPwd);
		String realPassWord =adminLoginService.adminlogin(account);
			System.out.println("welcome");
			if(loginPwd.equals(realPassWord)){
				System.out.println("管理员登陆成功");
				HttpSession session =request.getSession();
				session.setAttribute("loginName",loginName);
				return "admin/adminIndex";
			}else {
				System.out.println("输入账户名或者密码错误");
				return "admin/adminLogin";
			}
	
		} catch (Exception e) {
			System.out.println("你好"+e);
			e.printStackTrace();
		}
		return "admin/adminLogin";
	}
	
	
	@RequestMapping(value="/toLogin")
	public String toLogin(){
		return "admin/adminLogin";
	}
	

	
	/**
	 * 查询用户信息
	 */
	@RequestMapping(value="/showUser")
	public String QueryUser(String userAccount,String realname,Page page,Model model
	        ,HttpServletRequest request) {
		if(userAccount == null & realname == null){
	    Page p= userAccountService.findPage(page);
		page.setTotalCount(p.getTotalCount());
		page.setTotalPageCount(p.getTotalPageCount());
        List<AdminUserDto> list = userAccountService.adminQueryUser(p);
        model.addAttribute("userList",list);
        System.out.println(list);
		return "admin/userList";
	}else{
	    try {
	    realname = new String(request.getParameter("realname").getBytes("ISO8859-1"), "utf-8");
		Page p= userAccountService.findPage1(page,userAccount,realname);
		System.out.println(p.getTotalCount());
		page.setTotalCount(p.getTotalCount());
		page.setTotalPageCount(p.getTotalPageCount());
		model.addAttribute("userAccount", userAccount);
		model.addAttribute("realname", realname);
		List<AdminUserDto> list = userAccountService.adminQueryByCon(userAccount,realname,p);
		System.out.println(list);
		//像模型添加数据和结构
		model.addAttribute("userList" ,list);
		}
	    catch(Exception e){
	        System.out.println(e);
	        e.printStackTrace();
	    }
	    return "admin/userList";
	}
}
     /**
      * 删除用户信息
      */
	@RequestMapping(value="deleteUser")
	public String DeleteUser(UserAccount userAccount){
		userAccountService.deleteUser(userAccount.getUserAccount());
		System.out.println(userAccount);
		return  "redirect:/admin/showUser";
		}
	/**
	 * 恢复用户可用
	 */
	@RequestMapping(value="resumeUser")
	public String ResumeUser(UserAccount userAccount){
		userAccountService.resumeUser(userAccount.getUserAccount());
		return "redirect:/admin/showUser";
	}
	
	

	
	/**
	 * 查询所有商户信息
	 */
	@RequestMapping(value="/showSale")
	public String QuerySale(String saleAccount,String realname,Page page,Model model
	        ,HttpServletRequest request){
		if(saleAccount == null & realname == null){
	    Page p= saleAccountService.findPage(page);
		page.setTotalCount(p.getTotalCount());
		page.setTotalPageCount(p.getTotalPageCount());
        List<AdminSaleDto> list = saleAccountService.adminQuerySale(p);
        model.addAttribute("saleList",list);
        System.out.println(list);
		return "admin/saleList";
	}else{
	    try {
	    realname = new String(request.getParameter("realname").getBytes("ISO8859-1"), "utf-8");  
		Page p= saleAccountService.findPage1(page,saleAccount,realname);
		System.out.println(p.getTotalCount());
		page.setTotalCount(p.getTotalCount());
		page.setTotalPageCount(p.getTotalPageCount());
		model.addAttribute("saleAccount", saleAccount);
		model.addAttribute("realname", realname);
		List<AdminSaleDto> list = saleAccountService.saleQueryByCon(saleAccount,realname,p);
		System.out.println(list);
		//像模型添加数据和结构
		model.addAttribute("saleList" ,list);
		}catch(Exception e) {
		    e.printStackTrace();  
		}
		return "admin/saleList";
	}
}
	
	/**
     * 删除商户信息
     */
	@RequestMapping(value="deleteSale",method=RequestMethod.GET)
	public String DeleteSale(SaleAccount saleAccount){
		saleAccountService.deleteSale(saleAccount.getSaleAccount());
		return  "redirect:/admin/showSale";
		}
	
	/**
     * 恢复商户信息
     */
	@RequestMapping(value="resumeSale",method=RequestMethod.GET)
	public String ResumeSale(SaleAccount saleAccount){
		saleAccountService.resumeSale(saleAccount.getSaleAccount());
		return  "redirect:/admin/showSale";
		}
	

	/**
	 * 查询订单信息
	 */
	@RequestMapping(value="/showOrder")
	
	public String QueryOrder(Integer id,String realname,String state,Page page,
	        Model model,HttpServletRequest request){
	    if(id==null & realname==null & state==null) {
		Page p = orderService.findPage(page);
		page.setTotalCount(p.getTotalCount());
		page.setTotalPageCount(p.getTotalPageCount());
		List<AdminOrderDto> list = orderService.adminQueryOrder(p);
		request.setAttribute("orderList", list);
		return "admin/orderList";
	}
	  else {
	      try {
	            realname = new String(request.getParameter("realname").getBytes("ISO8859-1"),"utf-8");
	            Page p = orderService.findPage1(page,id,realname,state);
	            page.setTotalCount(p.getTotalCount());
	            page.setTotalPageCount(p.getTotalPageCount());
	            model.addAttribute("id", id);
	            model.addAttribute("realname",realname);
	            model.addAttribute("state",state);
	            List<AdminOrderDto> list = orderService.orderQueryByCon(id,realname,state,p);
	            model.addAttribute("orderList",list);
	            }
	           catch(Exception e) {
	               System.out.println("hello");
	               e.printStackTrace();
	           }
	            return "admin/orderList";   
	        } 
	    }
}
	

